package com.ruoyi.web.controller.openai;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.feign.KnowledgeClassifyServiceClient;
import com.ruoyi.system.feign.model.FeignKnowledgeClassifyModel;
import com.ruoyi.system.feign.result.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/openai/knowledgeClassify")
@RequiredArgsConstructor
public class KnowledgeClassifyController extends BaseController {
    private final KnowledgeClassifyServiceClient knowledgeClassifyServiceClient;
    private final static String prefix = "openai/knowledgeClassify";
    private final static Integer TRUE = 1;
    private final static Integer FALSE = 0;


    @GetMapping()
    public String knowledgeClassify() {
        return prefix + "/knowledgeClassify";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FeignKnowledgeClassifyModel query) {
        PageHelper<FeignKnowledgeClassifyModel> pageHelper = new PageHelper<>();
        pageHelper.setPager(new Pager().setPage_num(1).setPage_size(10));
        pageHelper.setSorter(new Sorter().setOrders(List.of(new Order().setProperty("created_at").setDirection("desc"))));
        pageHelper.setQuery(query);
        FeignDataResult<FeignKnowledgeClassifyModel> result = knowledgeClassifyServiceClient.find(pageHelper);
        if (result.isSuccess()){
            return getDataTable(result);
        }else{
            throw new ServiceException("获取服务接口数据发生内部错误");
        }

    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @Log(title = "知识库分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FeignKnowledgeClassifyModel model) {
        model.setUpdate_by(getSysUser().getUserName());
        model.setUpdated_at(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        model.setCreate_by(getSysUser().getUserName());
        model.setCreated_at(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        FeignObjectResult<FeignKnowledgeClassifyModel> result = knowledgeClassifyServiceClient.save(model);
        if (result.isSuccess()) {
            return toAjax(TRUE);
        }
        return toAjax(FALSE);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        FeignObjectResult<FeignKnowledgeClassifyModel> result = knowledgeClassifyServiceClient.detail(id);
        if (result.isSuccess()) {
            mmap.put("knowledgeClassify", result.getData());
        }
        return prefix + "/edit";
    }

    @Log(title = "知识库分类管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FeignKnowledgeClassifyModel model) {
        model.setUpdate_by(getSysUser().getUserName());
        model.setUpdated_at(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        FeignObjectResult<FeignKnowledgeClassifyModel> result = knowledgeClassifyServiceClient.update(model);
        if (result.isSuccess() && result.getData() != null) {
            return toAjax(TRUE);
        }
        return toAjax(FALSE);
    }

    @Log(title = "知识库分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FeignKnowledgeClassifyModel model) {
        FeignObjectResult<List<FeignKnowledgeClassifyModel>> result = knowledgeClassifyServiceClient.findAll(model);
        if (result.isSuccess()) {
            ExcelUtil<FeignKnowledgeClassifyModel> util = new ExcelUtil<>(FeignKnowledgeClassifyModel.class);
            return util.exportExcel(result.getData(), "知识库分类数据");
        }
        return null;
    }

    @Log(title = "知识库分类管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id) {
        FeignObjectResult<Boolean> result = knowledgeClassifyServiceClient.delete(id);
        if (result.isSuccess()) {
            return toAjax(TRUE);
        }
        return toAjax(FALSE);
    }


    @PostMapping("/checkUniqueByModel")
    @ResponseBody
    public boolean checkUniqueByModel(FeignKnowledgeClassifyModel model) {
        FeignObjectResult<Boolean> result = knowledgeClassifyServiceClient.exists(model);
        if (result.isSuccess()) {
            return result.getData() ? Boolean.FALSE : Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


    //===================================private=============================================
    private TableDataInfo getDataTable(FeignDataResult<FeignKnowledgeClassifyModel> result) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(result.getData().getContent() != null ? result.getData().getContent() : List.of());
        rspData.setTotal(result.getData().getPager().getTotal_record());
        return rspData;
    }

}
